<style>
    .layui-form-label{
        width: 100px;
    }
    .layui-input-block{
        margin-left: 130px;
    }
    .layui-input1{
        height: 38px;
        line-height: 1.3;
        border-width: 1px;
        border-color: #eee;
        border-style: solid;
        background-color: #fff;
        border-radius: 2px;
        padding-left: 10px;
        width: 50px;
    }
</style>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-list-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" >仓库</label>
                </div>
                <div class="layui-inline">
                    <select name="base_warehouse_id" class="selector" lay-search>
                    </select>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">SKU</label>
                    <div class="layui-input-block">
                        <input type="text" name="sku" placeholder="请输入SKU" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">库龄</label>
                    <div class="layui-input-block">
                        <input type="text" name="stock_age_s" onkeyup="clearNoNum(this)" placeholder=" > 0 " autocomplete="off" class="layui-input1"> ➖
                        <input type="text" name="stock_age_e" onkeyup="clearNoNum(this)" placeholder=" > 0 " autocomplete="off" class="layui-input1">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">SKU状态</label>
                    <div class="layui-input-block">
                        <select name="sku_status" class="selector">
                            <option value="">全部</option>
                            <option value="1">新产品</option>
                            <option value="2">清仓品</option>
                            <option value="3">在售品</option>
                            <option value="4">下架品</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin search" lay-submit lay-filter="LAY-list-search">搜索</button>
                    <button type="reset" class="reset layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-list"  lay-filter="LAY-list"></table>
            <!-- 头部工具栏 -->
            <script type="text/html" id="toolbar">
                <div class="layui-btn-group fl layui-btn-group-jy">
                    <a  class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-download-circle" lay-event="export">&nbsp;导出</a>
                </div>
            </script>
            <!-- 行内工具 -->
            <script type="text/html" id="LAY-list-toolbar">

            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'table', 'form','toolJ','laydate','upload'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , toolJ = layui.toolJ
            , laydate = layui.laydate
            , form = layui.form
            , user = layui.data('yujiso');
        //用户管理
        table.render({
            elem: '#LAY-list'
            ,url: '/index/Shopwarehousestock/getList'
            ,toolbar: '#toolbar'
            ,cellMinWidth:80
            ,defaultToolbar: ['filter']
            ,cols: [[
                {field: 'base_warehouse_name', title: '仓库', width:120}
                ,{field: 'sku', title: 'SKU'}
                ,{field: 'sku_name', title: 'SKU名称'}
                ,{field: 'sku_status_name', title: 'SKU状态'}
                ,{field: 'physical_stock', title: '实物库存'}
                ,{field: 'cost', title: '库存成本'}
                ,{field: 'price', title: '加权单价'}
                ,{field: 'stock_age', title: '库龄'}
            ]]
            ,page: true
            ,limit: 20
            ,text: {none: '暂无相关数据'}
        });

        //监听头部工具栏 操作
        table.on('toolbar(LAY-list)', function (obj) {
            //选中的数据
            var checkStatus = table.checkStatus('LAY-list')
                , data = checkStatus.data;
            //event对应模板的
            switch (obj.event) {
                case 'export':
                    var field = obj.config.where;
                    toolJ.ajax({"data": field,"url":"/index/Shopwarehousestock/export"});
                    break;
            }
        });

        //监听行内操作
        table.on('tool(LAY-list)', function(obj){
            var data = obj.data;
            switch (obj.event) {
                case 'disable':
                    toolJ.ajax({"data":{"id":data.id,"status":-1},"url":"/index/Shopwarehousestock/disableOrEnable"});
                    break;
                case 'enable':
                    toolJ.ajax({"data":{"id":data.id,"status":1},"url":"/index/Shopwarehousestock/disableOrEnable"});
                    break;
                case 'add':
                    layer.confirm('你真的要确定该条数据？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        toolJ.ajax({"data":{"id":data.id,"quantity":data.quantity,"base_warehouse_id":data.base_warehouse_id,
                            "sku":data.sku,"batch":data.batch,"business_sn":data.business_sn,"operator":user.cn_name,"is_sure":true},"url":"/index/Shopwarehousestock/edit"},function (res) {
                            var icon = 1;
                            if (res.code != 1) icon = 2;
                            layer.msg(res.msg, {icon: icon});
                            $('.search').click();
                        });
                    });
                    break;
                case 'edit':
                    admin.popup({
                        title: '编辑'
                        ,area: ['550px', '650px']
                        ,id: 'LAY-popup-content-add'
                        ,success: function(layero, index){
                            view(this.id).render('Shopwarehousestock/edit',data).done(function(){
                                form.render(null, 'layui-shopwarehousestock-edit-form-list');
                                //监听提交
                                form.on('submit(layuiadmin-app-form-submit)', function(data){
                                    var field = data.field; //获取提交的字段
                                    field['operator'] = user.cn_name;
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    toolJ.ajax({"data":field,"url":"/index/Shopwarehousestock/edit"});
                                    layui.table.reload('LAY-app-content-list'); //重载表格
                                    layer.close(index); //执行关闭
                                    table.reload();
                                });
                            });
                        }
                    });
                    break;
            }
        });

        /*动态给下拉框赋值*/
        var warehouse = layui.data(layui.setter.tableName).warehouse;
        var selectHtmlWarehouse = '<option value="">请选择</option>';
        for (var i_ in warehouse) {
            selectHtmlWarehouse += '<option value="' + i_ + '">' + warehouse[i_] + '</option>';
        }
        $("select[name='base_warehouse_id']").append(selectHtmlWarehouse);
        /*重置数据，刷新列表*/
        $('.reset').on('click', function () {
            $("input").val('');
            $("textarea").val('');
            $(".selector").val('');
            $('.search').click();
        });
        /*重新渲染界面*/
        form.render(null, 'LAY-list-form');
    });
</script>
<script type="text/javascript">
    /*库龄数据校验*/
    function clearNoNum(obj) {
        obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/^\./g, "");  //验证第一个字符是数字而不是.
//        obj.value = obj.value.replace(/^0*/g, "");  //验证第一个0清空.
        obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    }
</script>